package com.addplus.townmall.server.scheduled.scheduletask;

import com.addplus.townmall.server.api.mapper.town.*;
import com.addplus.townmall.server.api.model.town.*;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.addplus.townmall.server.api.utils.date.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 类名: CouponOnlineStatus
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/11/8 2:57 PM
 * @description 类描述: 优惠券上架状态更改定时器
 */
@Component
public class CouponOnlineStatus {

    private Logger logger = LoggerFactory.getLogger(CouponOnlineStatus.class);

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    OrderItemsFightMapper orderItemsFightMapper;

    @Autowired
    TownOrderFightMapper townOrderFightMapper;
    @Autowired
    MemberIntegralMapper memberIntegralMapper;

    /**
     * 优惠券上架超时任务
     */
    @Scheduled(cron = "0 0 12 * * ?")
    private void setCouponNotOnline() throws Exception {
        logger.info("开始执行优惠券上架超时任务");
        Date date = new Date();
        List<Coupon> onlineCoupon = couponMapper.selectList(new QueryWrapper<Coupon>()
                .eq("is_delete", 0).eq("online_status", 1));
        onlineCoupon.stream().forEach(o -> {
            if (o.getEndTime() != null) {

                Date date1 = o.getEndTime();
                Instant instant = date1.toInstant();
                ZoneId zoneId = ZoneId.systemDefault();
                LocalDateTime onlineTime = instant.atZone(zoneId).toLocalDateTime();
                /*LocalDateTime onlineTime = o.getEndTime();*/

                if (!DataUtils.isEmptyObject(onlineTime)) {
                    /* Integer groupTime = o.getEndTime();*/// 拼团时间
                    /*LocalDateTime overTime = onlineTime.plusHours(groupTime);*/// 结束时间
                    if (onlineTime.isBefore(LocalDateTime.now())) { // 结束时间大于当前时间
                        o.setOnlineStatus(0);// 设置为下架状态
                        couponMapper.updateById(o);
                    /*List<Integer> orderIds = orderItemsFightMapper.getOrderItemsFightByShopProductId(o.getId());
                    orderIds.stream().forEach(ob -> {
                        TownOrderFight townOrderFight = townOrderFightMapper.selectById(ob);
                        if (townOrderFight.getOrderStatus() == 2 && townOrderFight.getStatus() == 2) {
                            townOrderFight.setStatus(4);
                            townOrderFight.setOrderStatus(6);
                            townOrderFight.setCloseReason("未在指定时间内完成拼团。");
                            townOrderFightMapper.updateById(townOrderFight);
                        }
                        // 退款函数
                        // function 退款()
                    });*/
                    }

                }

            }

        });
        logger.info("优惠券上架超时任务结束");
    }

}
